'============================================================================================================
'  CheckVAShorts v1.1
'  Author:    Sizzl (cm@phore.net)
'  Modifed:  25/10/2010 - Initial build
'      18/04/2011 - Fix broken icon cache links
'
'  Script Function:
'    Check virtual app arrShortcuts which have been manually copied from their original location
'    and are no longer within management scope
'  
'  Usage:
'    wscript //nologo //b CheckVAShorts.vbs
'  
'  License:
'  
'  Licensed under the Apache License, Version 2.0 (the "License");
'  you may not use this file except in compliance with the License.
'  You may obtain a copy of the License at
'  
'  http://www.apache.org/licenses/LICENSE-2.0
'  
'  Unless required by applicable law or agreed to in writing, software
'  distributed under the License is distributed on an "AS IS" BASIS,
'  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
'  See the License for the specific language governing permissions and
'  limitations under the License.
'============================================================================================================
On Error Resume Next

const HKEY_CURRENT_USER = &H80000001
const HKEY_LOCAL_MACHINE = &H80000002
Dim strThisLocation: strThisLocation = Left(WScript.ScriptFullName,InStrRev(WScript.ScriptFullName,"\"))
Dim objShell: Set objShell = CreateObject("WScript.Shell")
Dim objFSO:  Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim strParentLocation: strParentLocation = objFSO.GetParentFolderName(strThisLocation)
Dim strComputer: strComputer = "."
Dim oReg: Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
'============================================================================================================
Dim arrShortcuts(150)
Dim i: i = 0
Dim strKeyPath: strKeyPath = "SOFTWARE\Microsoft\SoftGrid"
Dim UserPath, IconCache, AppIcon, AppVver
oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys
' Look for latest App-V version
For Each subkey In arrSubKeys
  AppVver = subKey
  oReg.GetStringValue HKEY_CURRENT_USER,strKeyPath & "\" & subKey & "\Client\UserInfo\","DataDirectory", UserPath
Next
If Len(UserPath) < 9 then
  UserPath = objShell.ExpandEnvironmentStrings("%APPDATA%") & "\SoftGrid Client"
End If
UserPath = objFSO.GetFolder(UserPath).ShortPath
IconCache = UserPath & "\ICONCA~1\"

If objFSO.FileExists(objShell.ExpandEnvironmentStrings("%TEMP%") & "\vapps.log") then objFSO.DeleteFile objShell.ExpandEnvironmentStrings("%TEMP%") & "\vapps.log"
' Check for non managed shortcuts and flag for update if required
' -- Desktop --
Set oFolder = objFSO.GetFolder(objShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop")
For Each oFile in oFolder.Files
  iUpdate = 0
  If LCase(Right(oFile.Name,"4")) = ".lnk" Then
    Set oShortcut = objShell.CreateShortcut(objFSO.GetAbsolutePathName(oFile))
    if InStr(LCase(oShortcut.targetpath),"sfttray.exe") then
      strLaunchApp = oShortcut.Arguments
      strLaunchApp = Replace(Right(strLaunchApp,Len(strLaunchApp)-8),"""","")
      if Left(oShortcut.IconLocation, Len(IconCache)) <> IconCache then iUpdate = 2
      if NOT objFSO.FileExists(Left(oShortcut.IconLocation,Len(oShortcut.IconLocation)-2)) then iUpdate = 2
      arrShortcuts(i) = array(oFile.Name,strLaunchApp,iUpdate,"",objFSO.GetAbsolutePathName(oFile))
      i = i + 1
    end if
  End If
Next
' -- Quick Launch --
Set oFolder = objFSO.GetFolder(objShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\Internet Explorer\Quick Launch")
For Each oFile in oFolder.Files
  If LCase(Right(oFile.Name,"4")) = ".lnk" Then
    Set oShortcut = objShell.CreateShortcut(objFSO.GetAbsolutePathName(oFile))
    if InStr(LCase(oShortcut.targetpath),"sfttray.exe") then
      strLaunchApp = oShortcut.Arguments
      strLaunchApp = Replace(Right(strLaunchApp,Len(strLaunchApp)-8),"""","")
      if Left(oShortcut.IconLocation, Len(IconCache)) <> IconCache then iUpdate = 2
      arrShortcuts(i) = array(oFile.Name,strLaunchApp,0,"",objFSO.GetAbsolutePathName(oFile))
      i = i + 1
    end if
  End If
Next
Set oFolder = Nothing
Dim j: j = i - 1
' Generate list of virtual apps
If NOT objFSO.FileExists(objShell.ExpandEnvironmentStrings("%TEMP%") & "\vapps.log") then ' Generate
  WScript.Echo "Generating Virtual App List..."
  objShell.Run "cmd /c sftmime /query obj:app /short /log " & objShell.ExpandEnvironmentStrings("%TEMP%") & "\vapps.log",0,true
  If objFSO.FileExists(objShell.ExpandEnvironmentStrings("%TEMP%") & "\vapps.log") then
    Dim objTS: Set objTS = objFSO.OpenTextFile(objShell.ExpandEnvironmentStrings("%TEMP%") & "\vapps.log",1,false)
    Do While objTS.AtEndOfStream = false
      strLine = objTS.ReadLine
      For i = 0 to j
        If InStr(strLine, arrShortcuts(i)(1)) then
          If arrShortcuts(i)(2) < 2 then
            arrShortcuts(i)(2) = 1
          End If
        Else
          strCompare = Left(arrShortcuts(i)(1),InStrRev(arrShortcuts(i)(1)," ")-1)
          if InStr(strLine,strCompare) then ' best bet
            arrShortcuts(i)(3) = strLine
          end if
        End If
      Next
    Loop
    objTS.Close
    Set objTS = Nothing
  End If
End If
For i = 0 to j
  AppIcon = ""
  If arrShortcuts(i)(2) = 0 then
    Set oShortcut = objShell.CreateShortcut(arrShortcuts(i)(4))
    if InStr(LCase(oShortcut.targetpath),"sfttray.exe") then
      oShortcut.Arguments = "/launch """ & arrShortcuts(i)(3) & """"
      oReg.GetStringValue HKEY_CURRENT_USER,strKeyPath & "\" & AppVver & "\Client\Applications\" & arrShortcuts(i)(3),"Icon", AppIcon
      If Len(AppIcon) > 0 then
        oShortCut.IconLocation = objFSO.GetFile(AppIcon).ShortPath & ",0"
      End If
      oShortcut.Save
      WScript.Echo "Updated shortcut: " & arrShortcuts(i)(0) & ", closest match: " & arrShortcuts(i)(3)
    end if
  ElseIf arrShortcuts(i)(2) = 2 then
    Set oShortcut = objShell.CreateShortcut(arrShortcuts(i)(4))
    if InStr(LCase(oShortcut.targetpath),"sfttray.exe") then
      if Left(oShortcut.IconLocation, Len(IconCache)) <> IconCache then
        oShortCut.IconLocation = IconCache & Mid(oShortcut.IconLocation,InStrRev(oShortcut.IconLocation,"\")+1,Len(oShortCut.IconLocation)-InStrRev(oShortcut.IconLocation,"\"))
      end if
      oReg.GetStringValue HKEY_CURRENT_USER,strKeyPath & "\" & AppVver & "\Client\Applications\" & arrShortcuts(i)(3),"Icon", AppIcon
      If Len(AppIcon) > 0 then
        oShortCut.IconLocation = objFSO.GetFile(AppIcon).ShortPath & ",0"
      End If
      oShortcut.Save
      WScript.Echo "Updated shortcut icon for: " & arrShortcuts(i)(0)
    end if
  End If
Next

